#include-once
#include 'Scintilla.au3'
#include 'ScintillaExt.au3'


$sACMacros = '@AppDataCommonDir?3 ' & _
'@AppDataDir?3 ' & _
'@AutoItExe?3 ' & _
'@AutoItPID?3 ' & _
'@AutoItVersion?3 ' & _
'@AutoItX64?3 ' & _
'@COM_EventObj?3 ' & _
'@CommonFilesDir?3 ' & _
'@Compiled?3 ' & _
'@ComputerName?3 ' & _
'@ComSpec?3 ' & _
'@CPUArch?3 ' & _
'@CR?3 ' & _
'@CRLF?3 ' & _
'@DesktopCommonDir?3 ' & _
'@DesktopDepth?3 ' & _
'@DesktopDir?3 ' & _
'@DesktopHeight?3 ' & _
'@DesktopRefresh?3 ' & _
'@DesktopWidth?3 ' & _
'@DocumentsCommonDir?3 ' & _
'@error?3 ' & _
'@exitCode?3 ' & _
'@exitMethod?3 ' & _
'@extended?3 ' & _
'@FavoritesCommonDir?3 ' & _
'@FavoritesDir?3 ' & _
'@GUI_CtrlHandle?3 ' & _
'@GUI_CtrlId?3 ' & _
'@GUI_DragFile?3 ' & _
'@GUI_DragId?3 ' & _
'@GUI_DropId?3 ' & _
'@GUI_WinHandle?3 ' & _
'@HomeDrive?3 ' & _
'@HomePath?3 ' & _
'@HomeShare?3 ' & _
'@HotKeyPressed?3 ' & _
'@HOUR?3 ' & _
'@IPAddress1?3 ' & _
'@IPAddress2?3 ' & _
'@IPAddress3?3 ' & _
'@IPAddress4?3 ' & _
'@KBLayout?3 ' & _
'@LF?3 ' & _
'@LocalAppDataDir?3 ' & _
'@LogonDNSDomain?3 ' & _
'@LogonDomain?3 ' & _
'@LogonServer?3 ' & _
'@MDAY?3 ' & _
'@MIN?3 ' & _
'@MON?3 ' & _
'@MSEC?3 ' & _
'@MUILang?3 ' & _
'@MyDocumentsDir?3 ' & _
'@NumParams?3 ' & _
'@OSArch?3 ' & _
'@OSBuild?3 ' & _
'@OSLang?3 ' & _
'@OSServicePack?3 ' & _
'@OSType?3 ' & _
'@OSVersion?3 ' & _
'@ProgramFilesDir?3 ' & _
'@ProgramsCommonDir?3 ' & _
'@ProgramsDir?3 ' & _
'@ScriptDir?3 ' & _
'@ScriptFullPath?3 ' & _
'@ScriptLineNumber?3 ' & _
'@ScriptName?3 ' & _
'@SEC?3 ' & _
'@StartMenuCommonDir?3 ' & _
'@StartMenuDir?3 ' & _
'@StartupCommonDir?3 ' & _
'@StartupDir?3 ' & _
'@SW_DISABLE?3 ' & _
'@SW_ENABLE?3 ' & _
'@SW_HIDE?3 ' & _
'@SW_LOCK?3 ' & _
'@SW_MAXIMIZE?3 ' & _
'@SW_MINIMIZE?3 ' & _
'@SW_RESTORE?3 ' & _
'@SW_SHOW?3 ' & _
'@SW_SHOWDEFAULT?3 ' & _
'@SW_SHOWMAXIMIZED?3 ' & _
'@SW_SHOWMINIMIZED?3 ' & _
'@SW_SHOWMINNOACTIVE?3 ' & _
'@SW_SHOWNA?3 ' & _
'@SW_SHOWNOACTIVATE?3 ' & _
'@SW_SHOWNORMAL?3 ' & _
'@SW_UNLOCK?3 ' & _
'@SystemDir?3 ' & _
'@TAB?3 ' & _
'@TempDir?3 ' & _
'@TRAY_ID?3 ' & _
'@TrayIconFlashing?3 ' & _
'@TrayIconVisible?3 ' & _
'@UserName?3 ' & _
'@UserProfileDir?3 ' & _
'@WDAY?3 ' & _
'@WindowsDir?3 ' & _
'@WorkingDir?3 ' & _
'@YDAY?3 ' & _
'@YEAR?3'
$sACPreprocessor = '#AutoIt3Wrapper_Add_Constants #AutoIt3Wrapper_Au3Check_Parameters #AutoIt3Wrapper_Au3Check_Stop_OnWarning'



Global Const $gnSCINTILLA_SCIPATH = 'Libraries\SciLexer.dll'
Scintilla_Init()



Func SetStyle($Sci, $style, $fore, $back = -1, $size = 0, $font = "", $bold = 0, $italic = 0, $underline = 0)
	_SendMessage($Sci, $SCI_STYLESETFORE, $style, $fore)
	If ($back = -1) Then
		$back = _SendMessage($Sci, $SCI_STYLEGETBACK, 32, 0)
	EndIf
	_SendMessage($Sci, $SCI_STYLESETBACK, $style, $back)
	If $size >= 1 Then
		_SendMessage($Sci, $SCI_STYLESETSIZE, $style, $size)
	EndIf
	If $font <> '' Then
		Scintilla_SendMessageStr($Sci, $SCI_STYLESETFONT, $style, $font)
	EndIf


	_SendMessage($Sci, $SCI_STYLESETBOLD, $style, $bold)
	_SendMessage($Sci, $SCI_STYLESETITALIC, $style, $italic)
	_SendMessage($Sci, $SCI_STYLESETUNDERLINE, $style, $underline)
EndFunc   ;==>SetStyle

Global Enum $gnSCINTILLAAU3_WORDLISTS_KEYWORDS, _
			$gnSCINTILLAAU3_WORDLISTS_FUNCTIONS, _
			$gnSCINTILLAAU3_WORDLISTS_MACROS, _
			$gnSCINTILLAAU3_WORDLISTS_SENTKEYS, _
			$gnSCINTILLAAU3_WORDLISTS_PREPROCESSORS, _
			$gnSCINTILLAAU3_WORDLISTS_SPECIAL, _
			$gnSCINTILLAAU3_WORDLISTS_EXPAND, _
			$gnSCINTILLAAU3_WORDLISTS_UDF, _
			$gnSCINTILLAAU3_WORDLISTS_NULL
$hWndTest = GUICreate('TEST')
$hScintilla = Scintilla_CreateEditor($hWndTest, 0, 0, 500, 300)
_SendMessage($hScintilla, $SCI_SETCODEPAGE, 1361, 0)
_SendMessage($hScintilla, $SCI_SETLEXER, $SCLEX_AU3, 0)
;~ _SendMessage($hScintilla, $SCI_STYLECLEARALL, 0, 0)

Scintilla_SendMessageStrW($hScintilla, $SCI_SETKEYWORDS, 0, 'func endfunc')
Scintilla_SendMessageStr($hScintilla, $SCI_SETKEYWORDS, 1, 'test')


SetStyle($hScintilla, 32, 0x000000, 0xF9F4F0, 9, 'Courier New')

SetStyle($hScintilla, $SCE_AU3_DEFAULT, 0x000000, 0xF9F4F0, 0, "")
SetStyle($hScintilla, $SCE_AU3_COMMENT, 0x008000, -1, 0, "", 0, 1)
SetStyle($hScintilla, $SCE_AU3_COMMENTBLOCK, 0x008000, -1, 0, "",  0, 1)
SetStyle($hScintilla, $SCE_AU3_NUMBER, 0xFF0000, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_FUNCTION, 0x900000, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_KEYWORD, 0xFF0000, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_MACRO, 0x808000, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_STRING, 0x0000FF, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_OPERATOR, 0x0080FF, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_VARIABLE, 0x5A5A5A, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_SENT, 0x808080, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_PREPROCESSOR, 0x808080, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_SPECIAL, 0xDC143C, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_EXPAND, 0x0000FF, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_COMOBJ, 0x993399, -1, 0, "")
SetStyle($hScintilla, $SCE_AU3_UDF, 0xFF8000, -1, 0, "")

_SendMessage($hScintilla, $SCI_SETSELFORE, True, 0x006000)
_SendMessage($hScintilla, $SCI_SETSELBACK, True, 0xA8A0F0)
_SendMessage($hScintilla, $SCI_SETSELALPHA, 50, 0)

_SendMessage($hScintilla, $SCI_INDICSETSTYLE, 8, 8)	;INDIC_STRAIGHTBOX
_SendMessage($hScintilla, $SCI_SETINDICATORCURRENT, 8, 0)
_SendMessage($hScintilla, $SCI_SETINDICATORVALUE, 1, 0)

_SendMessage($hScintilla, $SCI_MARKERDEFINE, 24, $SC_MARK_BACKGROUND)
_SendMessage($hScintilla, $SCI_MARKERSETFORE, 24, 0xE5E5E5)
_SendMessage($hScintilla, $SCI_MARKERSETBACK, 24, 0xCCCCCC)
_SendMessage($hScintilla, $SCI_MARKERADD, 0, 24)

_SendMessage($hScintilla, $SCI_SETMULTIPLESELECTION, True, 0)
_SendMessage($hScintilla, $SCI_SETADDITIONALSELECTIONTYPING, True, 0)


Scintilla_AutoCSetIgnoreCase($hScintilla, True)
Scintilla_SetReadOnly($hScintilla, True)
;SC_CASEINSENSITIVEBEHAVIOUR_RESPECTCASE=0
;~ Scintilla_AutoCSetCaseInsensitiveBehaviour($hScintilla, $SC_CASEINSENSITIVEBEHAVIOUR_IGNORECASE)
GUIRegisterMsg($WM_NOTIFY, __Wm_Notify)
GUISetState(@SW_SHOW, $hWndTest)

While GUIGetMsg() <> -3
	Sleep(10)
WEnd

Func __Wm_Notify($hWnd, $uMsg, $wParam, $lParam)
	$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
	$idFrom = DllStructGetData($tNMHDR, "IDFrom")
	$iCode = DllStructGetData($tNMHDR, "Code")
	$hWndFrom = DllStructGetData($tNMHDR, "HWNDFrom")

	If ($hWndFrom = $hScintilla) Then Return SciNotity($hScintilla, $iCode, $wParam, $lParam)
	Return 'GUI_RUNDEFMSG'
EndFunc

#cs
Calltip:
	- Description Str
	- Fn name
	- Part
		+ Start Pos
		+ End Pos
		+ Another calltip (can be null)
#ce


Func SciNotity($hScintilla, $iCode, $wParam, $lParam)
	Local Static $cOP = AscW('('), $cCP = AscW(')'), $cCM = AscW(','), _
				$cSP = AscW(' '), $cCR = AscW(@CR), $cLF = AscW(@LF), _
				$cAL = AscW('a'), $cZL = AscW('z'), $cAU = AscW('A'), $cZU = AscW('Z'), _
				$cD0 = AscW('0'), $cD9 = AscW('9'), $cUS = AscW('_'), $cDT = AscW('.'), _
				$cSH = AscW('#'), $cAS = AscW('@'), $cDL = AscW('$')	;Sharp, at-sign, dolla

	Local Static $sCallTip = '', $aCalltipParts[1][2], $nCurrentPart = 0, $nCalltipHltPos, $nCalltipStartPos = 0, $nCalltipEndPos = 9999
	Local Static $sAutoCompletePreset = '#AutoIt1 @123 Alpha Beta MsgBox'

	Local $tNMHDR = DllStructCreate($tagSCI_NOTIFICATION, $lParam)
	Switch $iCode
		Case $SCN_CHARADDED
			ConsoleWrite(@CRLF & '$SCN_CHARADDED')
			Local $cCh = DllStructGetData($tNMHDR, "ch")
			Local $nPos = Scintilla_GetCurrentPos($hScintilla)
;~ 			_SendMessage($hScintilla, $SCI_INDICATORFILLRANGE, 0, $nPos)
			Switch $cCh
				Case $cOP			; Parenthesis open
					ConsoleWrite('(')
					$sCallTip = 'MsgBox ( flag, "title", "text" [, timeout = 0 [, hwnd]] )'
					ReDim $aCalltipParts[5][2]
					$aCalltipParts[0][0] = 9
					$aCalltipParts[0][1] = 13
					$aCalltipParts[1][0] = 15
					$aCalltipParts[1][1] = 22
					$aCalltipParts[2][0] = 24
					$aCalltipParts[2][1] = 30
					$aCalltipParts[3][0] = 34
					$aCalltipParts[3][1] = 45
					$aCalltipParts[4][0] = 49
					$aCalltipParts[4][1] = 53
					$nCurrentPart = 0
				Case $cCP			; Parenthesis close
					ConsoleWrite(')')
				Case $cCR			; @CR
					If (Scintilla_GetEOLMode($hScintilla) = $SC_EOL_CR) Then
						__Scintilla_MacroAutoComplete($hScintilla)
					EndIf
				Case $cLF			; @LF
					If (Scintilla_GetEOLMode($hScintilla) <> $SC_EOL_CR) Then
						__Scintilla_MacroAutoComplete($hScintilla)
					EndIf
				Case $cCM			; comma (,)
					; If calltip is currently show, check if we need to shift to next parameter
					If (Scintilla_CalltipActive($hScintilla)) Then
						If ($nCurrentPart < UBound($aCalltipParts)-1) Then
							$nCurrentPart += 1
							Scintilla_CalltipSetHlt($hScintilla, _
									$aCalltipParts[$nCurrentPart][0], $aCalltipParts[$nCurrentPart][1])
							$nCalltipHltPos = $nPos
						Else
							Scintilla_CalltipSetHlt($hScintilla, StringLen($sCallTip)-1, StringLen($sCallTip))
						EndIf
					Else	; If calltip is currently un-showed, check if we should show it
						;SCI_GETSTYLEAT: check if current style is in comment or string
						$nCurrentPart = 0
						Scintilla_CalltipShow($hScintilla, 1, $sCallTip)
					EndIf
				Case $cSP			; space ( )
					ConsoleWrite(' ')

				Case $cDL			; dollar ($)
				Case $cSH, $cAS		; sharp (#), at-sign  (@)

				Case $cUS, $cAL To $cZL, $cAU To $cZU, $cD0 To $cD9					; Underscore, alphabet characters & digits (AutoIt 'word' char)
					;TODO: Set word char: _abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
					;Scintilla_SendMessageStrW($hScintilla, $SCI_AUTOCSHOW, 0, $sAutoCompletePreset)
					If Not Scintilla_SendMessageW($hScintilla, $SCI_AUTOCACTIVE, 0, 0) Then
						;Scintilla_AutoCShow($hScintilla, 1, $sAutoCompletePreset)
						;TEST
						; Here since we've just press a word char, so the state of words surrounding the current position
						; must be ...(w)*w|(w)*..., which can be one of two state described in the documentation
						; ....ww|ww...., ..ww|....ww... Either case we need to set $bOnlyWordCharacters to True to
						; find the correct word starting/ending position
						Local $nWordStart = Scintilla_WordStartPosition($hScintilla, $nPos, True)
						Local $nWordEnd = Scintilla_WordEndPosition($hScintilla, $nPos, True)
						; Retrive the first character of the word so we can find the list match with it
						Local $cFirstChar = Scintilla_GetCharAt($hScintilla, $nWordStart)
						; AutoIt functions, variables, macros, pre-processors's first character should not be a digit
						If ($cFirstChar < $cD0 Or $cFirstChar > $cD9) Then
							; Check the character right-before the word to see if its a macro, a pre-processor directive or a variable
							If ($nWordStart > 0) Then
								Switch Scintilla_GetCharAt($hScintilla, $nWordStart-1)
									Case $cAS			; Macro
										ConsoleWrite(@CRLF & 'Macro')
										Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart + 1, $sACMacros)
									Case $cSH			; Preprocessor
										ConsoleWrite(@CRLF & 'Preprocessor')
										Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart + 1, $sACPreprocessor)
									Case $cDL			; Variable
										ConsoleWrite(@CRLF & 'Variable')
										Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart + 1, _
													__ScintillaAu3_BuildACVariables())
									Case $cDT
										ConsoleWrite(@CRLF & 'Obj Property')
										Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart, _
													__ScintillaAu3_BuildACObjProperties())
									Case '+', '-', '*', '/', '&', '^', '(', '?', ':'
										;
										ContinueCase
									Case @CR
										;
										ContinueCase
									Case @LF
										;
										ContinueCase
									Case Else
										ConsoleWrite(@CRLF & 'Function 1')
										Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart, _
													__ScintillaAu3_BuildACFunctions())
								EndSwitch
							Else
								ConsoleWrite(@CRLF & 'Function 2')
								Scintilla_AutoCShow($hScintilla, $nPos - $nWordStart, _
											__ScintillaAu3_BuildACFunctions())
							EndIf

						EndIf
					EndIf



			EndSwitch
		Case $SCN_UPDATEUI
			Local $nPos = Scintilla_GetCurrentPos($hScintilla)
			Local $nUpdated = DllStructGetData($tNMHDR, "updated")
			Switch $nUpdated
				Case $SC_UPDATE_SELECTION
					ConsoleWrite($nPos)
					If (Scintilla_CalltipActive($hScintilla) _
								And $nPos >= $nCalltipStartPos And $nPos <= $nCalltipEndPos) Then
						If ($nPos < $nCalltipHltPos) Then
							; Caret move backward
						Else
							; Caret move forward
						EndIf
						$nCalltipHltPos = __Scintilla_GetPrevHighLightPos($hScintilla, $nPos)
						If ($nCurrentPart > 0) Then $nCurrentPart -= 1
						Scintilla_CalltipSetHlt($hScintilla, _
									$aCalltipParts[$nCurrentPart][0], $aCalltipParts[$nCurrentPart][1])
					Else
						Scintilla_CalltipCancel($hScintilla)
					EndIf
			EndSwitch
		Case $SCN_MODIFYATTEMPTRO
			If (Scintilla_LineFromPosition($hScintilla, Scintilla_GetCurrentPos($hScintilla)) = 0) Then
				; Check if any selection actived
				; Check if multiple selection is enabled
				; Drop readonly selection (opt, alter selection which exceed bounds)
				; Static
				; If any valid selection, set editable
				ConsoleWrite(@CRLF & 'RO' & ' ' & DllStructGetData($tNMHDR, 'position'))
				If (True) Then Scintilla_SetReadOnly($hScintilla, False)
			EndIf
		Case $SCN_MODIFIED
			Local $dwType = DllStructGetData($tNMHDR, 'modificationType')
			If (BitAND($dwType, 1) = 1 Or BitAND($dwType, 2) = 2) Then
				ConsoleWrite(@CRLF & '$SCN_MODIFIED')
				;SC_MOD_INSERTTEXT, SC_MOD_DELETETEXT
				ConsoleWrite(@CRLF & $dwType & ' ' & DllStructGetData($tNMHDR, 'position'))
				Scintilla_SetReadOnly($hScintilla, True)
			EndIf
	EndSwitch
EndFunc
Func __ScintillaAu3_BuildACVariables()
	Return ''
EndFunc

Func __ScintillaAu3_BuildACObjProperties()
	Return ''
EndFunc

Func __ScintillaAu3_BuildACFunctions()
	Return ''
EndFunc

; TODO: Another name, or break to 2 fns
; TODO: Else
Func __Scintilla_MacroAutoComplete($hScintilla, $nPos = -1)
	Local $nPrevLine = Scintilla_LineFromPosition($hScintilla, Scintilla_GetCurrentPos($hScintilla))-1
	Local $sPrevLine = Scintilla_GetLine($hScintilla, $nPrevLine)
	If ($nPos < 0) Then $nPos = Scintilla_GetCurrentPos($hScintilla)

	Local $aMatched = StringRegExp($sPrevLine, '^[\h]*', 2)
	Local $nLeadingSpaces = (IsArray($aMatched)? StringLen($aMatched[0]) : 0)
	Local $sLeadingSpaces = $nLeadingSpaces? $aMatched[0] : ''
	$sPrevLine = StringTrimLeft($sPrevLine, $nLeadingSpaces)
	Scintilla_AddText($hScintilla, $nLeadingSpaces, $sLeadingSpaces)
	$nPos += $nLeadingSpaces

	$aMatched = StringRegExp(StringLower($sPrevLine), '^(if|when|do|for|select|switch|with|func)([\s].*)', 2)
	If (IsArray($aMatched)) Then
		; TODO: Indent type: Tab, space (number of space characters)
		If True Then
			Local $nTabIndex = StringInStr($sLeadingSpaces, @TAB, 2, -1)
			Local $nModified = $nLeadingSpaces - $nTabIndex
			Scintilla_DeleteRange($hScintilla, $nPos-$nModified, $nModified)
			Scintilla_AddText($hScintilla, $nModified+1, @TAB & StringTrimLeft($sLeadingSpaces, $nTabIndex))
				$nPos += 1
		Else
			Local $nSpaces = 4
			Scintilla_AddText($hScintilla, $nLeadingSpaces+$nSpaces, _
						$sLeadingSpaces & StringFormat('%' & $nSpaces & 's', ' '))
			$nPos += $nSpaces
		EndIf

		; TODO: Option: Also complete sentences with default params
		Switch ($aMatched[1])
			Case 'if'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'EndIf')
			Case 'when'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'WEnd')
			Case 'do'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'Until')
			Case 'for'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'Next')
			Case 'select'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'EndSelect')
			Case 'switch'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'EndSwitch')
			Case 'with'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'EndWith')
			Case 'func'
				Scintilla_InsertText($hScintilla, $nPos, @CRLF & $sLeadingSpaces & 'EndFunc')
		EndSwitch
	EndIf
EndFunc

Func __Scintilla_GetPrevHighLightPos($hScintilla, $nPos = -1, $nFnPos = -1)
	If ($nPos < 0) Then $nPos = Scintilla_GetCurrentPos($hScintilla)
	If ($nFnPos < 0) Then $nFnPos = __Scintilla_GetFunctionPos($hScintilla, $nPos)

EndFunc


Func __Scintilla_GetFunctionPos($hScintilla, $nPos = -1)
	If ($nPos < 0) Then $nPos = Scintilla_GetCurrentPos($hScintilla)
	Local $nLine = Scintilla_LineFromPosition($hScintilla, $nPos)


	Local $nLineStart = Scintilla_PositionFromLine($hScintilla, $nLine)
	;fLocal $sText =
	Return 0
EndFunc

Func __Scintilla_GetNextHighLightPos($hScintilla, $nCurPos = -1, $nCurPart = -1)
	If ($nCurPos < 0) Then $nCurPos = Scintilla_GetCurrentPos($hScintilla)
EndFunc


Func SCI_GetTextRange($Sci, $start, $end)
	If $start > $end Then Return SetError(1, 0, "")
	Local $textRange = DllStructCreate($tagSCI_CHARACTERRANGE & "; ptr TextPtr; char Text[" & $end - $start + 1 & "]")
	DllStructSetData($textRange, 1, $start)
	DllStructSetData($textRange, 2, $end)
	DllStructSetData($textRange, 3, DllStructGetPtr($textRange, 4))
	Scintilla_SendMessageW($Sci, $SCI_GETTEXTRANGE, 0, DllStructGetPtr($textRange))
	Return DllStructGetData($textRange, "Text")
EndFunc   ;==>SCI_GETTEXTRANGE
Func SCI_GetCurrentWordEx($Sci, $onlyWordCharacters = 1, $CHARADDED = 0)

	Local $CurrentPos = Scintilla_GetCurrentPos($Sci)
	$CurrentPos -= ($CHARADDED = True)
	Return SCI_GetWordFromPos($Sci, $CurrentPos, $onlyWordCharacters)
EndFunc   ;==>SCI_GetCurrentWordEx
; Author: Prog@ndy
Func SCI_GetWordPositions($Sci, $CurrentPos, $onlyWordCharacters = 1)
	Local $Return[2] = [-1, -1]
	$Return[0] = Scintilla_SendMessageW($Sci, $SCI_WORDSTARTPOSITION, $CurrentPos, $onlyWordCharacters)
	$Return[1] = Scintilla_SendMessageW($Sci, $SCI_WORDENDPOSITION, $CurrentPos, $onlyWordCharacters)
	Return $Return
EndFunc   ;==>SCI_GetWordPositions
; Author: Prog@ndy
Func SCI_GetWordFromPos($Sci, $CurrentPos, $onlyWordCharacters = 1)
;~ 	Local $Return, $i, $Get, $char

;~ 	Local $CurrentPos = SCI_GetCurrentPos($Sci)
;~ 	$CurrentPos -= ($CHARADDED = True)
	Local $start = Scintilla_SendMessageW($Sci, $SCI_WORDSTARTPOSITION, $CurrentPos, $onlyWordCharacters)
	Local $end = Scintilla_SendMessageW($Sci, $SCI_WORDENDPOSITION, $CurrentPos, $onlyWordCharacters)
	Return SCI_GETTEXTRANGE($Sci, $start, $end)

EndFunc   ;==>SCI_GetWordFromPos



Func ScintillaEx_WordPositions($hScintilla, $nPos, $bOnlyWordChars = True)
EndFunc

Func ScintillaEx_IsWord($hScintilla, $nPos)
EndFunc

Func ScintillaAu3_PositionIsWordChar($hScintilla, $nPos)
	Local Static $cUS = Asc('_'), $cAL = Asc('a'), $cAU = Asc('A'), $cZL = Asc('z'), $cZU = Asc('Z'), $cD0 = Asc('0'), $cD9 = Asc('9')
	Local $cCh = Scintilla_GetCharAt($hScintilla, $nPos)
	Switch $cCh
		Case $cUS, $cD0 To $cD9, $cAL To $cZL, $cAU To $cZU
			Return True
		Case Else
			Return False
	EndSwitch
EndFunc